/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.cgb.portal.util;

import java.security.MessageDigest;
import java.util.Calendar;
import sun.misc.BASE64Encoder;

/**
 *
 * @author LUCAS
 */
public class EncryptionUtil {

    public static String generateActivationCode() {
        Calendar date = Calendar.getInstance();
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(date.toString().getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(digest.digest());
        } catch (Exception e) {
            return null;
        }
    }

    public static String encrypt(String s) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(digest.digest());
        } catch (Exception e) {
            return null;
        }
    }

    public static Boolean auth(String s, String password) {
        try {
            s = encrypt(s);
            if (s.equals(password)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }
}
